<?php
include_once("model/category.class.php");
include_once("model/user.class.php");
include_once("model/page.class.php");
include_once("model/product.class.php");
include_once("model/news.class.php");
//include_once("controller/upload.class.php");
include_once("controller/JKUpload.class.php");
include_once("includes/util.php");



/**
 * manage class. Process for all function in manage tab of admin page
 */
class Bcategory 
{
	var $db;
 	var $smarty;
 	
    function Bcategory($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/**
	 * Process Ajax call for add new category.
	 */
	function addNewCategory()
	{
		$cat_name 		 = null;
		$cat_parent 	 = 0;
		$cat_description = "";
		$active = 0;
		if(isset($_POST['total_photos']))
			$total_photos	= $_POST['total_photos'];
		if(isset($_POST['cat_type']))
			$cat_type 		 = @$_POST['cat_type'];
		if(isset($_POST['cat_manage']))
			$cat_manage 		 = @$_POST['cat_manage'];
		if(isset($_POST['cat_name']))
			$cat_name 		 = @$_POST['cat_name'];	
		if(isset($_POST['url']))
			$url 		 = @$_POST['url'];	
		if(isset($_POST['cat_parent']))	
			$cat_parent 	 = @$_POST['cat_parent'];
		if(isset($_POST['cat_description']))
			$cat_description = @$_POST['cat_description'];
		if(isset($_POST['cat_link']))
			$cat_link = @$_POST['cat_link'];
		if(isset($_POST['active']))
			$active = 1;		
			
		if($_FILES['cat_logo']['name'] != null && $_FILES['cat_logo']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['cat_logo']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_logo($_FILES['cat_logo']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
		if($_FILES['cat_flash']['name'] != null && $_FILES['cat_flash']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['cat_flash']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$flashname = $upload->upload_flash($_FILES['cat_flash']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
		
		$listcat = category::get_category_by_level($this->db,$cat_parent,'');
		$cat_position = count($listcat)+1;	
		$cat = array('cat_name' => $cat_name,
						 'cat_parent' => $cat_parent,
						 'url' => $url,
						 'cat_logo'=>$imagename,
						 'cat_flash'=>$flashname,
						 'cat_link'=>$cat_link,
						 'cat_type'=>$cat_type,
						 'cat_manage'=>$cat_manage,
						 'cat_position' => $cat_position,
						 'active'=>$active,
						 'cat_description' => $cat_description);
						 
		$ret = category::validate($this->db, $cat);
		
		if($ret == ""){
			$id = category::saveCategory($this->db, $cat);
			if($total_photos>0){
					for($i=1;$i<=$total_photos;$i++){
						if($_FILES['slide_image_'.$i]['name'] != NULL && $_FILES['slide_image_'.$i]['name'] != "") {
							$upload = new JKUpload();
							$image = $_FILES['slide_image_'.$i]['name'];
							$path = pathinfo($image);
							$image_type = strtolower($path['extension']);
							$namefile = explode(".".$image_type,$_FILES['slide_image_'.$i]['name']);
							if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
								$imagename = $upload->upload_logo($_FILES['slide_image_'.$i]['tmp_name'], $namefile['0'], $image_type);
							} 
							$slideid = category::saveSlidePhotos($this->db, $id,$imagename);
						}	
						
					}
				}	
			$cat['new_name'] = "";
			
			if($id > 0){
				$msg = sprintf(_("Category <u>%s</u> has been added."), 
										$cat['new_name']);
			}else{
				$msg = _('Could not insert new category to database.');
			}
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=manage&sub1=category") ;
		}else{
			
			setcookie("message",$ret,time()+5);
			header("Location: ./?mod=manage&sub1=editcategory&act=newcategory") ;	
		}
	}
	
	/**
	 * Process Ajax call for update a category
	 */
	function editCategory(){
		
//		after process we must show category page
		 
		$cat_id			 = -1;
		$cat_name 		 = null;
		$cat_parent 	 = 0;
		$cat_description = "";
		if(isset($_POST['total_photos']))
			$total_photos	= $_POST['total_photos'];
		if(isset($_POST['cat_id']))
			$cat_id 		 = @$_POST['cat_id'];
		if(isset($_POST['cat_manage']))
			$cat_manage 		 = @$_POST['cat_manage'];
		if(isset($_POST['cat_type']))
			$cat_type 		 = @$_POST['cat_type'];
		if(isset($_POST['cat_name']))
			$cat_name 		 = @$_POST['cat_name'];	
		if(isset($_POST['url']))
			$url 		 = @$_POST['url'];		
		if(isset($_POST['cat_parent']))	
			$cat_parent 	 = @$_POST['cat_parent'];
		if(isset($_POST['cat_description']))
			$cat_description = @$_POST['cat_description'];
		if(isset($_POST['cat_link']))
			$cat_link = @$_POST['cat_link'];
		if(isset($_POST['active']))
			$active = 1;	
		
		$catinfo = category::getCategoryById($this->db,$cat_id);
		if($_FILES['cat_logo']['name'] != null && $_FILES['cat_logo']['name'] != "") {
			$upload = new JKUpload();
			$image = $_FILES['cat_logo']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_logo($_FILES['cat_logo']['tmp_name'], $namefile['0'], $image_type);
				@unlink("../".UPLOAD_DIR_LOGO.$catinfo->cat_logo);
			} else {
				$_SESSION['file_error'] = $this->l['error']['imageError'];
			}
		}
		else
			$imagename = $catinfo->cat_logo;
			
		if($_FILES['cat_flash']['name'] != null && $_FILES['cat_flash']['name'] != "") {
			$upload = new JKUpload();
			$image = $_FILES['cat_flash']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$flashname = $upload->upload_flash($_FILES['cat_flash']['tmp_name'], $namefile['0'], $image_type);
				@unlink("../".UPLOAD_FILES.$catinfo->cat_flash);
			} else {
				$_SESSION['file_error'] = $this->l['error']['imageError'];
			}
		}
		else
			$flashname = $catinfo->cat_flash;	
			
		$cat = array('cat_id' => $cat_id,
					 'cat_name' => $cat_name,
					 'cat_parent' => $cat_parent,
					 'cat_logo'=>$imagename,
					 'cat_flash'=>$flashname,
					 'cat_link'=>$cat_link,
					  'url'=>$url,
					 'cat_type'=>$cat_type,
					  'cat_manage'=>$cat_manage,
					 'active' => $active,
					 'cat_description' => $cat_description);
					 
		$msgId = 2;
		$msg = category::validate($this->db, $cat);
		//no error
		//echo $msg;
		if($msg == ""){
			$res = category::updateCategory($this->db, $cat);
			if($total_photos>0){
					for($i=1;$i<=$total_photos;$i++){
						if($_FILES['slide_image_'.$i]['name'] != NULL && $_FILES['slide_image_'.$i]['name'] != "") {
							$upload = new JKUpload();
							$image = $_FILES['slide_image_'.$i]['name'];
							$path = pathinfo($image);
							$image_type = strtolower($path['extension']);
							$namefile = explode(".".$image_type,$_FILES['slide_image_'.$i]['name']);
							if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
								$imagename = $upload->upload_logo($_FILES['slide_image_'.$i]['tmp_name'], $namefile['0'], $image_type);
							} 
							$slideid = category::saveSlidePhotos($this->db, $cat_id ,$imagename);
						}	
						
					}
				}	
			
			
			if($res == true || $slideid > 0 ){
				$msg = sprintf(_("Category <u>%s</u> has been added."),$cat_name);
			}else{
				$msg = _('Could not update category to database.');
			}
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=manage&sub1=category") ;
		}else{
			setcookie("message",$ret,time()+5);
			header("Location: ./?mod=manage&sub1=editcategory") ;	
		}

	}
	
	public function deleteCategory() {
		$ID = $_POST['id'];
		$old_pos = $_POST['cat_position'];
			if(isset($_POST['catparent']) && $_POST['catparent']!="")
				$id = $_POST['catparent'];
			else
				$id = 0;
			$listcat = category::get_category_by_level($this->db,$id,$ID);	
			
			if($listcat!="") {
				foreach($listcat as $item) {
					if($item->cat_position>$old_pos) {
						$new_position = $item->cat_position -1;
						category::update_position_category($this->db,$item->cat_ID,$new_position);
					}
				}	
			}
		
		$photos = category::PhotosByCatId($this->db,$ID);
		
		//// DELETE PHOTOS FOR SILDE CATEGORY
		//////////////////////////////////////
		if($photos!=''){
			foreach($photos as $item){
				@unlink("../".UPLOAD_DIR_LOGO.$item->photo);	
			}
			category::deletePhotoCatByCatID($this->db,$ID);	
		}
		
		$path = category::getCategoryById($this->db,$ID);	
		@unlink("../".UPLOAD_DIR_LOGO.$path->cat_logo);	
		
		$res = category::deleteCategory($this->db, $ID);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res) {
			$arr['message'] = _("Category list has been updated!");
			setcookie("message", $message,time()+5);
			$arr['code'] = 0;
		} else {
			$arr['message'] = _("Could not delete category, id: ") . $ID;
			unset($_SESSION['message']);
		}
		
		echo array2json($arr);
	}
	
	public function change_position() {
		$old_pos = $_POST['position'];
		$cat_id = $_POST['cat_id'];
		if(isset($_POST['cat_parent']) && $_POST['cat_parent']!="")
			$id = $_POST['cat_parent'];
		else
			$id = 0;
		$listcat = category::get_category_by_level($this->db,$id,$_POST['cat_id']);
		if($_POST['type_change'] == 'first') {
			foreach($listcat as $item) {
				if($item->cat_position<$old_pos) {
					$new_position = $item->cat_position +1;
					category::update_position_category($this->db,$item->cat_ID,$new_position);
				}
			}	
			category::update_position_category($this->db,$cat_id,'1');	
		}
		else if($_POST['type_change'] == 'last') {
			foreach($listcat as $item) {
				if($item->cat_position>$old_pos) {
					$new_position = $item->cat_position-1;
					category::update_position_category($this->db,$item->cat_ID,$new_position);
				}
			}
			$newpos = count($listcat)+1;
			category::update_position_category($this->db,$cat_id,$newpos);				
		}
		else if($_POST['type_change'] == 'after') {
			$after_id = $_POST['position_id'];
			foreach($listcat as $item) {
				if($old_pos>$after_id) {
					if($item->cat_position>$after_id && $item->cat_position<$old_pos) {
						$new_position = $item->cat_position+1;
						category::update_position_category($this->db,$item->cat_ID,$new_position);	
					}
				} else {
					if($item->cat_position<=$after_id && $item->cat_position>$old_pos) {
						$new_position = $item->cat_position-1;
						category::update_position_category($this->db,$item->cat_ID,$new_position);	
					}
				}	
			}	
			if($old_pos>$after_id)
				category::update_position_category($this->db,$cat_id,$after_id+1);
			else
				category::update_position_category($this->db,$cat_id,$after_id);
	
		}
		//Back URL after change position
	
		header("Location: ./?mod=manage&sub1=category");
	}
	
	
	public function Delete_Photo(){
		$arr = array('code' => 0, 'message' => "");
		if(isset($_POST['id']) && $_POST['id']!="")
				$id = $_POST['id'];
		if(isset($_POST['name']) && $_POST['name']!="")
				$path = $_POST['name'];
		$res = category::deletePhotoCat($this->db,$id);
		@unlink("../".UPLOAD_DIR_LOGO.$path);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Photo <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Photo <u>%s</u>."), $id);
		}
		echo array2json($arr);		 
	}
}
?>